<template>
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <div class="dialog-table">
        <page-filter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
        >
          <template v-slot:filter-button>
            <div class="dialog-table-header">
              <div class="header-left">
                <div class="lable">产品名称：{{ info.commonName }}</div>
                <div class="lable">
                  消耗单价：{{ $fn.TripartiteMethod(info.consumePrice) }}
                </div>
              </div>
              <div class="header-right">
                <div class="scan-groups">
                  <el-input
                    v-model="filterInfo.query.uniqueCodeLike"
                    :clearable="true"
                    placeholder="请输入个体码"
                    @keyup.enter.native="search"
                    @input="
                      (e) => (filterInfo.query.uniqueCodeLike = validSe(e))
                    "
                  ></el-input>
                  <el-button style="margin-left: 10px" @click="search"
                    >搜索</el-button
                  >
                </div>
              </div>
            </div>
          </template>
        </page-filter>
      </div>
      <el-form :model="tableInfo" ref="tableForm" style="width: 100%">
        <dialog-table
          :refresh="tableInfo.refresh"
          :init-curpage="tableInfo.initCurpage"
          v-model:data="tableInfo.data"
          :api="api"
          :check-box="false"
          :tab-index="true"
          :listen-height="false"
          :pager="false"
          :query="filterInfo.query"
          :field-list="tableInfo.fieldList"
          @handleEvent="handleEvent"
        >
          <template v-slot:col-frozenQty="scope">
            <el-input-number
              v-model="scope.row.frozenQty"
              controls-position="right"
              :precision="0"
              v-enterNumber
              :max="999999999"
              :min="0"
            ></el-input-number>
          </template>
        </dialog-table>
      </el-form>
    </page-dialog>
    <chose-printer :visible="printerInfo.visible" @callBack="printerCallBack" />
  </div>
</template>

<script setup name="IndividualDetails">
import PageDialog from "@/components/PageDialog/index";
import PageFilter from "@/components/PageFilter/index";
import chosePrinter from "@/components/ChosePrinter/index";
import { withTableReceivingOrderDetailDetailAll } from "@/api/stock";
import DialogTable from "@/components/DialogTable/index";

const { proxy } = getCurrentInstance();
const emit = defineEmits();

const props = defineProps({
  api: {
    type: Function,
    default: withTableReceivingOrderDetailDetailAll,
  },
  showDialog: {
    type: Boolean,
  },
  pageStatus: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  info: {
    type: Object,
    default: function () {
      return {};
    },
  },
});

// 过滤相关配置
const filterInfo = reactive({
  query: {
    ...props.query,
  },
  list: [{ type: "slot", value: "button" }],
});
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [
    { label: "个体码", value: "uniqueCode" },
    { label: "原厂码", value: "factorySubCode" },
    { label: "生产批号", value: "batchNumber" },
    { label: "生产日期", value: "productionDate" },
    { label: "失效日期", value: "validityDate" },
    { label: "病人名称", value: "patientName" },
    { label: "住院号", value: "patientNo" },
    { label: "消耗时间", value: "consumeTime" },
    {
      label: "消耗数量",
      value: "consumeQty",
      type: "column",
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      label: "收货数量",
      value: "qty",
      type: "column",
      formatter: proxy.$fn.qtyFormatter,
    },
  ],
});
// 弹窗相关
const dialogInfo = reactive({
  title: "个体详情",
  btLoading: false,
  btList: [
    { label: "关闭", event: handleClose, show: true },
    { label: "保存", event: handleSave, show: true },
  ],
});
const printerInfo = reactive({
  visible: false,
  data: {},
});
const selectRow = ref("");
const scanInput = ref("");

const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emit("update:showDialog", newValue); // 更新父组件shows
  },
});

// 获取列表
function getList() {
  tableInfo.refresh = Math.random();
}
function search() {
  
  tableInfo.initCurpage = Math.random();
  tableInfo.refresh = Math.random();
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false;
  emit("reset");
}
function sumArr(arr) {
  let sum = 0;
  arr.map((i) => {
    sum += Number(i.frozenQty);
  });
  return sum;
}
function handleSave() {
  const qtyTotal = sumArr(tableInfo.selections);
  const param = {
    ...props.info,
    qty: qtyTotal,
    details: [...tableInfo.selections],
  };
  emit("callback", param);
  handleClose();
}
function printerCallBack(printer) {
  if (printer !== false) {
    proxy.$print.buildRenderBarCode(printerInfo.data, printer);
  }
  printerInfo.visible = false;
}
function handleEvent(event, data) {
  
  switch (event) {
    case "tableCheck":
      tableInfo.selections = [...data];
      break;
  }
}
</script>

<style scoped lang="scss">
:deep(.filter-item) {
  width: 100%;
}
.text-style {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.scan-groups {
  display: flex !important;
  align-items: center;
}
</style>
